package com.fsj.spring.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.fsj.spring.model.TUser;
import com.fsj.spring.service.IUserService;
import com.fsj.spring.util.Constants;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;

/*
 * 不需要实现任何接口，也不需要继承任何的类，也不需要任何 Servlet API
 */
@Controller
@RequestMapping("/welcome")
//将Model中属性名为Constants.USER_INFO_SESSION的属性放到Session属性列表中，以便这个属性可以跨请求访问
@SessionAttributes(Constants.USER_INFO_SESSION)
public class WelcomeController {
 private static final Logger log =LogManager.getLogger(WelcomeController.class);
//    @Autowired(required = true)
//     @Qualifier("userService")
//     @Resource(name="userService") 
    @Resource
    public IUserService userService;

    public IUserService getUserService() {
        return userService;
    }

    //ServletContext servletContext =this.getServletContext();
    //WebApplicationContext wac = WebApplicationContextUtils.
    //getRequiredWebApplicationContext(servletContext);
    //User user = (User)wac.getBean("user");
    @RequestMapping(value = "index")
    public void index(HttpServletRequest req, HttpServletResponse res) throws Exception {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        try {
            /* TODO output your page here*/
            out.println("welcome long");
            out.println("you are good ");

        } finally {
            out.close();
        }
    }

    @RequestMapping(value = "home")
    public String Home() throws Exception {
        return "relogin";
    }

//    @Autowired
//    @Qualifier
//    @Resource
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String helloWorld(TUser user, Model model) throws Exception {

        TUser user1 = userService.getUserByName(user.getName());
        if (user1 == null) {
            model.addAttribute("message", "用户不存在");
            log.debug("用户不存在"+user.getName());
            return "relogin";
        } else if (user.getPassword() == null || !user.getPassword().equals(user1.getPassword())) {
            model.addAttribute("message", "密码错误");
            log.debug("pwd error");
            return "relogin";
        } else {
            model.addAttribute(Constants.USER_INFO_SESSION, user1); //名为Constants.USER_INFO_SESSION的属性放到Session属性列表中
            log.debug("login success");
            return "welcome";
        }
    }
}
